<template>
	<view class="content wrap">
		<scan></scan>
		<scanner></scanner>
		<sm></sm>
		<honeywell></honeywell>
		<u-form :model="form" ref="uForm">
			<u-form-item label="起点" label-width="150" prop="srcPositionCode">
				<u-row>
					<u-col span="11" text-align="center">
						<u-input v-model="form.srcPositionCode" placeholder="请输入起点" />
					</u-col>
					<u-col span="1" text-align="center" style="display: flex; align-items: center;" @click="scan">
						<u-icon name="scan" color="#2b85e4" size="43" style="font-size: 80px;">手机扫描</u-icon>
					</u-col>
				</u-row>
			</u-form-item>

			<u-form-item label="终点" label-width="150" prop="tarPositionCode">
				<u-row>
					<u-col span="11" text-align="center">
						<u-input v-model="form.tarPositionCode" placeholder="请输入终点" @blur="getMapStorageByRoadway"/>
					</u-col>
					<u-col span="1" text-align="center" style="display: flex; align-items: center;" @click="scan1">
						<u-icon name="scan" color="#2b85e4" size="43" style="font-size: 80px;">手机扫描</u-icon>
					</u-col>
				</u-row>
			</u-form-item>
			
			<u-form-item label="优先级" label-width="150" prop="tarPositionCode">
				<u-row>
					<u-col span="11" text-align="center">
						<u-input v-model="form.priority" placeholder="请输入优先级" />
					</u-col>
				</u-row>
			</u-form-item>
		</u-form>
		<view>
			<u-form-item>
			</u-form-item>
		</view>
		<u-button @click="clockin" type="primary">提交</u-button>
		<view>
			<u-toast ref="uToast" />
		</view>
		<view class="u-border-bottom" style="margin-top: 20rpx;background: #e5e9f2;">
			<view class="u-demo-block__content u-border-bottom">
				<u-row
						justify="space-between"
						customStyle="margin-bottom: 10px"
				>
					<u-col
							span="6"
					>
						<view class="demo-layout">{{'巷道名称：' + item.areaName}}</view>
					</u-col>
					<u-col
							span="6"
					>
						<view class="demo-layout">{{'优先级：' + item.priority}}</view>
					</u-col>
				</u-row>
				<u-row
						justify="space-between"
						customStyle="margin-bottom: 10px"
				>
					<u-col
							span="6"
					>
						<view class="demo-layout">{{'储位数量：' + item.allNum}}</view>
					</u-col>
					<u-col
							span="6"
					>
						<view class="demo-layout">{{'已用数量：' + item.fullNum}}</view>
					</u-col>
				</u-row>
				<u-row
						justify="space-between"
						customStyle="margin-bottom: 10px"
				>
					<u-col
							span="6"
					>
						<view class="demo-layout">{{'锁定数量：' + item.lockNum}}</view>
					</u-col>
					<u-col
							span="6"
					>
						<view class="demo-layout">{{'空余数量：' + item.emptyNum}}</view>
					</u-col>
				</u-row>
		
			</view>
		</view>
	</view>
</template>

<script>
	import scan from "@/components/uni-scan/scan.vue"
	import scanner from "@/components/uni-scan/scanner.vue"
	import sm from "@/components/uni-scan/sm.vue"
	import honeywell from "@/components/uni-scan/honeywell.vue"

	export default {
		components: {
			scanner,
			scan,
			sm,
			honeywell
		},
		data() {
			return {
				list:[],
				item:{
					areaName:'',
					priority:'',
					allNum:'',
					fullNum:'',
					lockNum:'',
					emptyNum:'',
				},
				form: {
					srcPositionCode: "", //开始库位
					tarPositionCode: "", //目标位置
					priority:10
				},
				scanneal: [],
			};
		},
		onShow() {
			let srcPositionCodeObtained = false;
			let tarPositionCodeObtained = false;

			uni.$off('scan');
			uni.$on('scan', (res) => {
				console.log('扫码结果：', res.codeStr);

				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.codeStr;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					// this.form.tarPositionCode = res.codeStr;
					this.getTarPositionCode(res.codeStr);
					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			});

			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', (res) => {
				console.log('扫码结果：', res.code);
				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.code;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					// this.form.tarPositionCode = res.code;
					this.getTarPositionCode(res.code);
					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			})
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', (res) => {
				console.log('扫码结果：', res.code);
				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.code;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					// this.form.tarPositionCode = res.code;
					this.getTarPositionCode(res.code);
					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			})
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', (res) => {
				console.log('扫码结果：', res.code);
				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.code;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					// this.form.tarPositionCode = res.code;
					this.getTarPositionCode(res.code);
					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			})
		},

		methods: {
			scan() {
				uni.scanCode({
					success: (res) => {
						this.form.srcPositionCode = res.result;
					}
				});
			},
			scan1() {
				uni.scanCode({
					success: (res) => {
						// this.form.tarPositionCode = res.result;
						this.getTarPositionCode(res.result);
					}
				});
			},
			getTarPositionCode(codeStr){
				if (codeStr.includes('{')) {
					let match = codeStr.match(/{(.*?)}/); // 使用正则表达式匹配{}中的内容
					if (match) {
						let dataInBraces = match[1]; // 获取{}中的数据
						let parsedData = JSON.parse('{' + dataInBraces + '}'); // 解析{}中的JSON数据
						if (parsedData && parsedData['channel_name']) {
							let channel_name = parsedData['channel_name'];
							this.form.tarPositionCode = "H"+channel_name.substring(0, 3);
							// 进行后续处理
							this.getMapStorageByRoadway();
						} else {
							console.log('无法获取商品条码字段');
						}
					} else {
						console.log('未找到{}中的数据');
					}
				} else {
					console.log('直接使用扫描到的数据：', codeStr);
					this.form.tarPositionCode = codeStr;
				
				}
			},
			clockin() {
				if (typeof this.form.srcPositionCode == 'undefined' || this.form.srcPositionCode == null || this.form
					.srcPositionCode ==
					'') {
					uni.showToast({
						title: '请输入起点',
						duration: 2000,
						icon: 'none',
					});
					return false;
				}
				if (typeof this.form.tarPositionCode == 'undefined' || this.form.tarPositionCode == null || this.form
					.tarPositionCode == '') {
					uni.showToast({
						title: '请输入终点',
						duration: 2000,
						icon: 'none',
					});
					return false;
				}
				uni.showLoading({
					title: '正在提交'
				})

				this.$http.post('/rest/tuskAgvTaskController/addItem', {}, {
					params: this.form,
					header: {

						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if (res.data.code == 200) {
						this.$refs.uToast.show({
							title: '操作成功',
							duration: 3000,
							type: 'success'
						})
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							duration: 3000,
							type: 'error'
						})
					}
				})
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				console.log(e);
			},
			getMapStorageByRoadway(){
				if (typeof this.form.tarPositionCode == 'undefined' || this.form.tarPositionCode == null || this.form
					.tarPositionCode == '') {
					return false;
				}
				this.$http.post('/rest/tuskAgvTaskController/getMapStorageByRoadway', {}, {
					params: {
						 roadway: this.form.tarPositionCode,
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if (res.statusCode == 200) {
						if(res.data.code == 200){
							this.list = res.data.data;
							if(this.list.length > 0 ){
								this.item = this.list[0];
							}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.wrap {
		padding: 30rpx;
	}
</style>